package com.appcenter.application.controller;

import com.appcenter.application.input.RolePermission.GetRolePermissionsInput;
import com.appcenter.application.input.RolePermission.PageLoadInput;
import com.appcenter.application.input.RolePermission.SaveRolePermissionsInput;
import com.appcenter.application.service.RolePermissionService;
import com.appcenter.jz.config.ErrorJsonResult;
import com.appcenter.jz.config.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("api/services/jzappcenterservice/rolePermission")
@Api("角色权限操作API")
public class RolePermissionController {

    @Autowired
    RolePermissionService rolePermissionService;


    @ApiOperation(value = "搜索权限", notes = "搜索权限",produces = "application/json")
    @PostMapping("/getrolePermissions")
    public ResponseEntity<JsonResult> getRolePermissions(@RequestBody GetRolePermissionsInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        JsonResult r = new JsonResult();
        try {
            r.setResult(rolePermissionService.GetRolePermissions(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }

        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "加载角色权限信息", notes = "加载角色权限信息",produces = "application/json")
    @PostMapping("/pageLoad")
    public ResponseEntity<JsonResult> pageLoad(@RequestBody PageLoadInput input) throws Exception {
        JsonResult r = new JsonResult();
        try {
            r.setResult(rolePermissionService.PageLoad(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }

        return ResponseEntity.ok(r);
    }


    @ApiOperation(value = "保存角色权限信息", notes = "保存角色权限信息",produces = "application/json")
    @PostMapping("/saveRolePermissions")
    public ResponseEntity<JsonResult> saveRolePermissions(@RequestBody SaveRolePermissionsInput input) throws Exception {
        JsonResult r = new JsonResult();

        try {
            r.setResult(rolePermissionService.SaveRolePermissions(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }

        return ResponseEntity.ok(r);
    }

}
